package com.scau.wuwei.library_system.core.controller;

import com.scau.wuwei.library_system.core.bean.UserInfo;
import com.scau.wuwei.library_system.core.repository.UserInfoRepository;
import com.scau.wuwei.library_system.core.service.PasswordHelper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.crypto.RandomNumberGenerator;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping(value = "/user")
public class UserInfoController {
    @Autowired
    private UserInfoRepository userInfoRepository;
    @Autowired
    private PasswordHelper passwordHelper = new PasswordHelper();

    /**
     * 显示所有用户数
     * @return
     */
    @GetMapping(value = "/list")
    @RequiresPermissions(value = "user:view")
    public String userList(HttpServletRequest request) {
        List<UserInfo> users = userInfoRepository.findAll();
        request.setAttribute("users", users);
        return "users";
    }

    /**
     * 添加用户
     * @param name
     * @param username
     * @return
     */
   /* @PostMapping(value = "/add")
    @RequiresPermissions(value = "user:add")
    public UserInfo userAdd(@RequestParam("name") String name,
                            @RequestParam("username") String username) {
        UserInfo userInfo = new UserInfo();
        userInfo.setName(name);
        userInfo.setUsername(username);

        return userInfoRepository.save(userInfo);
    }*/

    /**
     * 查询一个用户
     * @param id
     * @return
     */
    @ResponseBody
    @GetMapping(value = "/uid/{id}")
    @RequiresPermissions(value = "user:query")
    public UserInfo userFindOne(@PathVariable("id") long id) {
        return userInfoRepository.findOne(id);
    }

    /**
     * 删除用户
     * @param id
     */
    @DeleteMapping(value = "/del/{id}")
    @RequiresPermissions(value = "user:del")
    public void userDelete(@PathVariable("id") long id) {
        userInfoRepository.delete(id);
    }

    /**
     * 通过学号、工号查询
     * @param name
     * @return
     */
    @ResponseBody
    @GetMapping(value = "/username/{username}")
    @RequiresPermissions(value = "user:query")
    public UserInfo findUserByName(@PathVariable("name") String name) {
        return userInfoRepository.findByName(name);
    }

    /**
     * 添加用户
     * @param request
     * @return
     */
    @PostMapping(value = "/add")
    public String userAdd(HttpServletRequest request)  {
        System.out.println("UserInfoController.register()   POST");
        String name = (String)request.getParameter("name");
        String password = (String)request.getParameter("name"); //密码初始是学号
        String username = (String)request.getParameter("username");
        String class_name = (String)request.getParameter("class");
        String department = (String)request.getParameter("department");

        String sex_female = (String)request.getParameter("sex_female");
        String sex_male = (String)request.getParameter("sex_male");
        String sex;

        UserInfo user = new UserInfo();
        user.setName(name);
        user.setUsername(username);
        user.setPassword(password);
        passwordHelper.encryptPassword(user);

        user.setClass_name(class_name);
        if (sex_female.equals("on")) {
            sex = "女";
        } else if (sex_male.equals("on")) {
            sex = "男";
        } else {
            sex = "男";
        }
        user.setSex(sex);
        user.setDepartment(department);
        user.setState((byte)0);
        System.out.print(user.toString() + "!!!!!!!!!");
        userInfoRepository.save(user);
        return "users";
    }

    @GetMapping(value = "/add")
    public String userAdd() {
        return "register";
    }
}
